#!/bin/sh

# save current working directory
CWD=`pwd`

# make sure we have a few required programs
which patch > /dev/null || (echo "Can't find patch"; exit 1)
which tar > /dev/null || (echo "Can't find tar"; exit 1)
which wget > /dev/null || (echo "Can't find wget"; exit 1)

# make sure we have MingW
which i586-mingw32msvc-gcc > /dev/null || (echo "Can't find MingW gcc"; exit 1)
which i586-mingw32msvc-ar > /dev/null || (echo "Can't find MingW ar"; exit 1)

# download source code
cd src || exit 1
wget http://yawt.googlecode.com/files/AutoHotkey104614_source.exe || exit 1
wget http://yawt.googlecode.com/files/cabextract-1.2.tar.gz || exit 1
wget http://yawt.googlecode.com/files/cabextract.patch || exit 1
wget http://yawt.googlecode.com/files/unzip.patch || exit 1
wget http://yawt.googlecode.com/files/unzip552.tgz || exit 1

tar zxf cabextract-1.2.tar.gz || exit 1
tar zxf unzip552.tgz || exit 1
patch -p0 < cabextract.patch || exit 1
patch -p0 < unzip.patch || exit 1

cd cabextract-1.2 || exit 1
CC=i586-mingw32msvc-gcc RANLIB="i586-mingw32msvc-ar -s" ./configure --host win32

# go back to the original current working directory
cd "$CWD"
